/**
 * 初始化VPN服务器管理详情对话框
 */
var VpnServerInfoDlg = {
    vpnServerInfoData: {}
};

/**
 * 清除数据
 */
VpnServerInfoDlg.clearData = function () {
    this.vpnServerInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
VpnServerInfoDlg.set = function (key, val) {
    this.vpnServerInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
VpnServerInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
VpnServerInfoDlg.close = function () {
    parent.layer.close(window.parent.VpnServer.layerIndex);
}

/**
 * 收集数据
 */
VpnServerInfoDlg.collectData = function () {
    this
        .set('id')
        .set('serverIp')
        .set('serverPort')
        .set('serverName')
        .set('pubKey')
        .set('status')
        .set('createTime')
        .set("beginSegment")
        .set("endSegment")
        .set("serverName2")
        .set("serverName3")
        .set("sourcePath")
        .set("serverType")
        .set("areaLevel");
}

/**
 * 添加VPN服务器节点
 */
VpnServerInfoDlg.addVPNSever = function () {
    this.clearData();
    this.collectData();

    var serverIp = $.trim($("#serverIp").val());
    var serverPort = $.trim($("#serverPort").val());
    var serverName = $.trim($("#serverName").val());
    var serverName2 = $.trim($("#serverName2").val());
    var serverName3 = $.trim($("#serverName3").val());
    var pubKey = $.trim($("#pubKey").val());
    var status = $.trim($("#status").val());
    var beginSegment = $.trim($("#beginSegment").val());
    var endSegment  = $.trim($("#endSegment").val());
    var serverType = $.trim($("#serverType").val());
    var areaLevel = $("#areaLevel").val();
    var crow_file = $("#crowdImg")[0].files[0];


    if(serverName == ''){
        layer.msg("请填写简体名称");
    }
    else if(serverName2 == ''){
        layer.msg("请填写英文名称")
    }
    else if(serverName3 == ''){
        layer.msg("请填写繁体名称")
    }
    else if(serverIp == ''){
        layer.msg("IP地址不能为空")
    }
    else if(pubKey == ''){
        layer.msg("公钥不能为空")
    }
    else if(serverPort == ''){
        layer.msg("监听端口不能为空")
    }
    else if(beginSegment == ''){
        layer.msg("开始网段不能为空")
    }
    else if(endSegment == ''){
        layer.msg("结束网段不能为空")
    }
    else if(areaLevel == null || areaLevel === '') {
        layer.msg("区域等级不能为空")
    }
    else if(crow_file == null){
        layer.msg("请选择图标")
    }else{

        var formData =  new FormData;

        formData.append("file",crow_file);
        formData.append("serverName",serverName);
        formData.append("serverName2",serverName2);
        formData.append("serverName3",serverName3);
        formData.append("serverIp",serverIp);
        formData.append("serverPort",serverPort);
        formData.append("pubKey",pubKey);
        formData.append("serverType",serverType);
        formData.append("status",status);
        formData.append("beginSegment",beginSegment);
        formData.append("endSegment",endSegment);
        formData.append("areaLevel", areaLevel);

        var loadIndex;
        var confirmIndex = layer.confirm("确定添加VPN服务器?",
            {btn:['确认添加','取消添加']},
            function () {
                layer.closeAll()
                loadIndex = layer.load(0,{
                    shade:false
                });
                setTimeout(function () {
                    addNewVpnServer(formData);
                },1000);
            },function () {
                layer.msg("取消添加",{icon:1})
            });
    }
}

function addNewVpnServer(formData){
    $.ajax({
        url: Feng.ctxPath + "/vpnServer/add",
        dataType:'json',
        type:'POST',
        async: false,
        data: formData,
        processData : false, // 使数据不做处理
        contentType : false, // 不要设置Content-Type请求头
        success: function(data){
            console.log(data);
            if(data.code == 200){
                Feng.success("添加成功!");
                window.parent.VpnServer.table.refresh();
                VpnServerInfoDlg.close();
            }
            layer.closeAll();
        },
        error:function(response){
            console.log(response);
            Feng.error(response.responseJSON.message);
            layer.closeAll();
        }
    });
}

/**
 * 修改VPN服务器信息
 */
VpnServerInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();
    //图片
    var crow_file = $("#crowdImg")[0].files[0];

    var id = $.trim($("#id").val());
    var serverName = $.trim($("#serverName").val());
    var serverName2 = $.trim($("#serverName2").val());
    var serverName3 = $.trim($("#serverName3").val());
    var serverIp = $.trim($("#serverIp").val());
    var serverPort = $.trim($("#serverPort").val());
    var pubKey = $.trim($("#pubKey").val());
    var isFree = $.trim($("#serverType").val());
    var areaLevel = $("#areaLevel").val();

    var formData = new FormData();

    if(crow_file != null){
        formData.append("file",crow_file);
    }
    formData.append("serverId",id);
    formData.append("serverName",serverName);
    formData.append("serverName2",serverName2);
    formData.append("serverName3",serverName3);
    formData.append("serverIp",serverIp);
    formData.append("serverPort",serverPort);
    formData.append("pubKey",pubKey);
    formData.append("serverType",isFree);
    formData.append("areaLevel", areaLevel);

    var loadIndex;
    layer.confirm("确定要修改VPN服务器信息",
        {btn:['确认修改','取消修改']},
        function () {
            layer.closeAll()
            loadIndex = layer.load(0,{
                shade:false
            });
            setTimeout(function () {
                $.ajax({
                    url: Feng.ctxPath + "/vpnServer/update",
                    dataType:'json',
                    type:'POST',
                    async: false,
                    data: formData,
                    processData : false, // 使数据不做处理
                    contentType : false, // 不要设置Content-Type请求头
                    success: function(data){
                        console.log(data);
                        if(data.code == 200){
                            Feng.success("修改成功!");
                            window.parent.VpnServer.table.refresh();
                            VpnServerInfoDlg.close();
                        }
                        layer.closeAll();
                    },
                    error:function(response){
                        console.log(response);
                        Feng.error(response.responseJSON.message);
                        layer.closeAll();
                    }
                });
            },1000);
        },function () {
            layer.msg("取消修改",{icon:1})
        }
    );
};

$(function () {
    //监听是否选择图片
    $("#crowdImg").change(function () {
        if(this.files != null){
            var filePath = URL.createObjectURL(this.files[0]);
            $("#preview").attr("src",filePath);
        }
    })

});

